﻿using LdtJudge.DataEngine.DataBaseStores;
using LdtJudge.DataEngine.Model.DataModel.DataParseStorage;
using LdtJudge.DataEngine.Model.DataModel.SysCommon;
using Sunny.UI;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Text;
using System.Windows.Forms;
using System.Xml;

namespace Ldt.WindowsFormsApp
{
    public partial class FormShowData : UIForm
    {
        private DataContext _dataContext;
        public FormShowData(DataContext dataContext)
        {
            InitializeComponent();
            _dataContext = dataContext;
            LoadTreeViewData();
        }
        
        /// <summary>
        /// 加载treeview数据
        /// </summary>
        private void LoadTreeViewData()
        {
            TreeNode systemConfig = new TreeNode("配置信息");
            uiTreeView1.Nodes.Add(systemConfig);
            TreeNode analysisResult = new TreeNode("分析结果信息");
            analysisResult.Nodes.Add(new TreeNode("分析结果") { Name="analysisxml",Tag= _dataContext.DataLayerAns.AnalysisResultsXml });
            analysisResult.Nodes.Add(new TreeNode("案件实体") { Name = "anjianshiti",Tag = _dataContext.DataLayerAns.Anjianshitis });
            analysisResult.Nodes.Add(new TreeNode("实体角色") { Name = "shitijuese",Tag = _dataContext.DataLayerAns.Anjianshitijueses });
            analysisResult.Nodes.Add(new TreeNode("案由信息") { Name = "casecause",Tag = _dataContext.DataLayerAns.CaseCausesList });
            analysisResult.Nodes.Add(new TreeNode("案由要素") { Name = "caselement",Tag = _dataContext.DataLayerAns.ElementAnalysisResults });
            uiTreeView1.Nodes.Add(analysisResult);
            TreeNode optimizeResult = new TreeNode("优选结果信息");
            optimizeResult.Nodes.Add(new TreeNode("优选结果") { Name = "optimizationresult",Tag = _dataContext.DataLayerOpt.OptimizationXml });
            uiTreeView1.Nodes.Add(optimizeResult);
        }
        private void button1_Click(object sender, EventArgs e)
        {
            string strReadLine = "";
            double number = 0.00;
            // 读取文件的源路径及其读取流
            string strReadFilePath = @"F:\Untitled-4.txt";
            StreamReader reader = new StreamReader(strReadFilePath, Encoding.GetEncoding("utf-8"));//用此格式汉字不会乱码
            strReadLine = reader.ReadToEnd().Replace(@"\r","").Replace("\n",""); //读取所有数据
            var Listvalue = strReadLine.Split(new string[] { "RegexMessage,执行时间为：" }, StringSplitOptions.None);
            int qcode = 0;
            int bcode = 0;
            int wcode = 0;
            int wnumber = 0;
            int ynumber = 0;
            foreach (var str in Listvalue)
            {
              var nunberlist=  str.Split(new string[] { " \r\r" }, StringSplitOptions.None);
                if (nunberlist.Length>0)
                {
                    if (nunberlist[0].Length<10)
                    {
                        if (Convert.ToDouble(nunberlist[0])>1000)
                        {
                            qcode++;
                        }
                        if (Convert.ToDouble(nunberlist[0])>100)
                        {
                            bcode++;
                        }
                        if (Convert.ToDouble(nunberlist[0]) > 50 && Convert.ToDouble(nunberlist[0])<100)
                        {
                            wcode++;
                        }
                        number = number + Convert.ToDouble(nunberlist[0]);
                    }
                }

                var nrlist = str.Split(new string[] { " RegexMessage, 匹配值为：" }, StringSplitOptions.None); 
                var contextList=nrlist[1].Split(new string[] { "* * *" }, StringSplitOptions.None);
                if (contextList[0].ToString()!= "\"\" \r\r")
                {
                    wnumber++;
                }
                else
                {
                    ynumber++;

                }
            }
        }

        private void uiTreeView1_AfterSelect(object sender, TreeViewEventArgs e)
        {
            if (e.Node != null)
            {
                if (e.Node.Tag != null)
                {
                    var tagType = e.Node.Tag.GetType();
                    if (tagType.Name == "XmlDocument")
                        uiRichTextBox1.Text = ShowXml((e.Node.Tag as XmlDocument).OuterXml);
                    switch (e.Node.Name.ToLower())
                    {
                        case "anjianshiti":
                            uiDataGridView1.DataSource = e.Node.Tag as List<An_anjianshiti>;
                            break;
                        case "shitijuese":
                            uiDataGridView1.DataSource = e.Node.Tag as List<An_anjianshitijuese>;
                            break;
                        case "casecause":
                            uiDataGridView1.DataSource = e.Node.Tag as List<CaseCauseModel>;
                            break;
                        default:
                            break;
                    }
                }
            }
        }
        /// <summary>
        /// XML格式化为文本显示
        /// </summary>
        /// <param name="str"></param>
        /// <returns></returns>
        public string ShowXml(string str)
        {
            MemoryStream mstream = new MemoryStream();
            XmlTextWriter writer = new XmlTextWriter(mstream, null);
            XmlDocument xmldoc = new XmlDocument();
            writer.Formatting = Formatting.Indented;

            xmldoc.LoadXml(str);
            xmldoc.WriteTo(writer);
            writer.Flush();
            writer.Close();

            Encoding encoding = Encoding.GetEncoding("utf-8");
            string strReturn = encoding.GetString(mstream.ToArray());
            mstream.Close();
            return strReturn;
        }
    }
}
